#!/bin/bash
#ORACLE_USER=${ORACLE_USER}
#UNIX_GROUP_NAME=${UNIX_GROUP_NAME}
#INVENTORY_LOCATION=${INVENTORY_LOCATION}
#ORACLE_BASE=${ORACLE_BASE}
#ORACLE_HOME=${ORACLE_HOME}
#GRID_BASE=${GRID_BASE}
#GRID_HOME=${GRID_HOME}
#OSDBA_GROUP=${OSDBA_GROUP}
#OSOPER_GROUP=${OSOPER_GROUP}
#OSBACKUPDBA_GROUP=${OSBACKUPDBA_GROUP}
#OSDGDBA_GROUP=${OSDGDBA_GROUP}
#OSKMDBA_GROUP=${OSKMDBA_GROUP}
#OSRACDBA_GROUP=${OSRACDBA_GROUP}

#CLUSTER_NODES=oracle19crac01,oracle19crac02
HAS_ERROR=0
function DO_CMD() {
    echo Exec command: $@
    $@
    EXIT_CODE=$?
    if [ $EXIT_CODE != 0 ]; then
        HAS_ERROR=1
        echo ERROR: Execute $@ failed.
        exit $EXIT_CODE
    else
        echo FINE: Execute success.
    fi
}

function SUDO_ORA_CMD() {
    USER=$1
    shift
    #echo命令行隐藏密码
    CMD_SECTXT=$(echo "$@" | perl -pe 's/Password=\S*/Password=******/g' | perl -pe 's/Password\s+\S*/Password ******/g')
    echo "Exec command: su - '$USER' -c '$CMD_SECTXT'"
    eval "su - '$USER' -c 'AUTOEXEC_JOBID=$AUTOEXEC_JOBID $@'"
    exitCode=$?
    if [ $exitCode -ge 128 ]; then
        #负数返回码，大于127就是负数的补码
        echo "ERROR: Installation failed, exitCode:$exitCode."
        exit $exitCode
    elif [ $exitCode -gt 0 ]; then
        echo "WARN: Installation has some warning, exitCode:$exitCode."
    else
        echo "FINE: All installations were successful."
    fi
}

function setInstallEnv() {
    #输入的EXEC_ENV是textarea格式，把符号'\n'替换为真正的换行符号
    #然后eval为shell的变量
    EXEC_ENV_TXT=$(echo "$EXEC_ENV" | perl -pe 's/\\n/\n/g' | perl -pe 's/\s*=\s*/=/g')
    if [ -n "$EXEC_ENV_TXT" ]; then
        eval "$EXEC_ENV_TXT"
    else
        echo "ERROR: Execute env is empty, must defined by option --EXEC_ENV."
        exit 2
    fi
}

#参数处理,全部用长参数
######################################
function parseOpts() {
    OPT_SPEC=":h-:"
    while getopts "$OPT_SPEC" optchar; do
        case "${optchar}" in
        -)
            case "${OPTARG}" in
            CV_ASSUME_DISTID)
                CV_ASSUME_DISTID="${!OPTIND}"
                OPTIND=$(($OPTIND + 1))
                ;;
            INSTALLER_DIR)
                INSTALLER_DIR="${!OPTIND}"
                OPTIND=$(($OPTIND + 1))
                ;;
            RESPONSE_FILE)
                RESPONSE_FILE="${!OPTIND}"
                OPTIND=$(($OPTIND + 1))
                ;;
            EXEC_ENV)
                EXEC_ENV="${!OPTIND}"
                OPTIND=$(($OPTIND + 1))
                ;;
            CLUSTER_NODES)
                CLUSTER_NODES="${!OPTIND}"
                OPTIND=$(($OPTIND + 1))
                ;;
            *)
                if [ "$OPTERR" = 1 ] && [ "${OPT_SPEC:0:1}" != ":" ]; then
                    echo "Unknown option --${OPTARG}" >&2
                fi
                ;;
            esac
            ;;
        h)
            usage
            exit 2
            ;;
        *)
            if [ "$OPTERR" != 1 ] || [ "${OPT_SPEC:0:1}" = ":" ]; then
                echo "Non-option argument: '-${OPTARG}'" >&2
            fi
            ;;
        esac
    done
}

parseOpts "$@"
setInstallEnv

#把参数里的响应文件拷贝到reponse目录，作为静默安装响应文件的基础响应，参数定义部分属性会覆盖此响应文件
if [ -n "$RESPONSE_FILE" ]; then
    if [ ! -e "$RESPONSE_FILE" ]; then
        echo "ERROR: Response file:$RESPONSE_FILE not exists."
        exit 3
    else
        REAL_RESPONSE_FILE=$ORACLE_HOME/install/response/db_auto.rsp
        echo "Copy $RESPONSE_FILE to $REAL_RESPONSE_FILE."
        cp $RESPONSE_FILE "$REAL_RESPONSE_FILE" && chown $GRID_USER:$UNIX_GROUP_NAME "$REAL_RESPONSE_FILE"
        if [ $? != 0 ]; then
            echo "ERROR: Copy $RESPONSE_FILE to $REAL_RESPONSE_FILE failed."
            exit 4
        fi
    fi
else
    echo "ERROR: Must defined response file with option --RESPONSE_FILE."
    exit 5
fi

ENV_DISTID=""
if [ "$CV_ASSUME_DISTID" != "AUTO" ]; then
    ENV_DISTID="CV_ASSUME_DISTID=$CV_ASSUME_DISTID"
fi

if [ -e "$ORACLE_HOME/runInstaller" ]; then
    INSTALLER_DIR=$ORACLE_HOME
elif [ -e "$ORACLE_HOME/oui/bin/runInstaller" ]; then
    INSTALLER_DIR="$ORACLE_HOME/oui/bin"
fi

if [ ! -e "$INSTALLER_DIR/runInstaller" ]; then
    echo "ERROR: Install script $INSTALLER_DIR/runInstaller not exists.\n"
    exit 6
fi

##runInstall exit code##
# 0	All installations were successful.
# 1	All installations were successful, but some optional configuration tools failed.
# 2	Local installations were successful, but some remote operations failed.
# 3	All installations were successful, but some recommended configuration tools failed.
# 4	The installation was stopped.
# 6	The installation was successful after you proceeded by disregarding a few prerequisite checks or warnings.
# -1	At least one installation failed.
# -2	The installation failed. One or more validation of variables failed.
# -3	The attempted installation encountered a prerequisite failure. Some of the optional prerequisites have not been met. See the logs for details.
# You can ignore this code if the prerequisite is optional.

#静默安装DB软件
SUDO_ORA_CMD $ORACLE_USER "cd \"$INSTALLER_DIR\" && $ENV_DISTID ./runInstaller" -silent -force -noconfig -ignorePrereq \
    -responseFile $REAL_RESPONSE_FILE \
    oracle.install.option=INSTALL_DB_SWONLY \
    UNIX_GROUP_NAME=$UNIX_GROUP_NAME \
    INVENTORY_LOCATION=$INVENTORY_LOCATION \
    ORACLE_BASE=$ORACLE_BASE \
    ORACLE_HOME=$ORACLE_HOME \
    oracle.install.db.InstallEdition=EE \
    oracle.install.db.OSDBA_GROUP=$OSDBA_GROUP \
    oracle.install.db.OSOPER_GROUP=$OSOPER_GROUP \
    oracle.install.db.OSBACKUPDBA_GROUP=$OSBACKUPDBA_GROUP \
    oracle.install.db.OSDGDBA_GROUP=$OSDGDBA_GROUP \
    oracle.install.db.OSKMDBA_GROUP=$OSKMDBA_GROUP \
    oracle.install.db.OSRACDBA_GROUP=$OSRACDBA_GROUP \
    oracle.install.db.CLUSTER_NODES=$CLUSTER_NODES

exit $HAS_ERROR
